package org.example.documenttool.service;

import com.lowagie.text.DocumentException;
import org.docx4j.convert.in.xhtml.XHTMLImporterImpl;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.example.documenttool.entity.OssAliyun;
import org.example.documenttool.utils.OssUtils;
import org.springframework.stereotype.Service;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;

@Service
public class FileToolService {
    /**
     * 将html转成pdf
     */
    public Path htmlToPdf(String htmlPDF, String pdfFilePath) throws IOException {
        //        TODO 生成路径
        Path pdfPath=Path.of("/usr/local/DemoFile/pdf/"+pdfFilePath+".pdf");
        // 确保目录存在，如果不存在则创建
        Files.createDirectories(pdfPath.getParent());
        // 临时文件保存 HTML
        Path tempHtmlFile = Files.createTempFile("temp", ".html");
        Files.writeString(tempHtmlFile, htmlPDF);

        try (OutputStream os = new FileOutputStream(String.valueOf(pdfPath))) {
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocument(tempHtmlFile.toFile());
            renderer.layout();
            renderer.createPDF(os);
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
        // 删除临时html文件
        Files.deleteIfExists(tempHtmlFile);
        return pdfPath;
    }
    /**
     * 将html转word
     */
    public String wordToHtml(String html, String wordFilePath) throws IOException, Docx4JException {
//        TODO 空白word
        WordprocessingMLPackage wordprocessingMLPackage=WordprocessingMLPackage.createPackage();
//        TODO 使用docx4j将html导入word
        XHTMLImporterImpl xhtmlImporter=new XHTMLImporterImpl(wordprocessingMLPackage);
        wordprocessingMLPackage.getMainDocumentPart()
                .getContent()
                .addAll(xhtmlImporter.convert(html,null));
//        TODO 输出保存文件
        Path path=Files.createTempFile("word-", ".docx");
        wordprocessingMLPackage.save(path.toFile());
        String ossName= wordFilePath+".docx";
        if (OssUtils.isOssEnabled(path,ossName))
        {
            Files.deleteIfExists(path);
            return OssAliyun.ENDPOINT+"/"+ossName;
        }else {
            Files.deleteIfExists(path);
            return "no!!!";
        }
    }
    /**
     * html转pdf并存储到OSS
     */
    public String htmlToPdfOSS(String html,String ossFilePath) throws IOException {
        Path tempPdf = Files.createTempFile("pdf-", ".pdf");
        try {
            try (OutputStream out = Files.newOutputStream(tempPdf)) {
                ITextRenderer renderer = new ITextRenderer();
                renderer.setDocumentFromString(html);                      // 如果有本地资源，可用 setDocument(tempHtml)
                renderer.layout();
                renderer.createPDF(out);                                   // 写入磁盘，而不是内存
            }
            //TODO 拼接OSS路径
            String ossName= ossFilePath+".pdf";
            if (OssUtils.isOssEnabled(tempPdf,ossName))
            {
                Files.deleteIfExists(tempPdf);
                return OssAliyun.ENDPOINT+"/"+ossFilePath+".pdf";
            }else {
                Files.deleteIfExists(tempPdf);
                return "no!!!";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "no";
        }
    }
    /**
     * 制作ppt
     */

}
